import axios from 'axios'
import router from '@/router';


const service = axios.create({
    timeout: 5000
});

//添加请求拦截器
axios.interceptors.request.use(function (config) {
    //将token存进请求头的Authorization。
    if(window.localStorage.getItem("userInfo") != null){
        config.headers.Authorization = window.localStorage.getItem("token");
    }
    return config
}, function (error) {
  return Promise.reject(error)
});


//添加响应拦截器
axios.interceptors.response.use(function (response) {
    // console.log('响应拦截器', response)
    // console.log("响应返回："+response)
    // console.log(response)
    return response
}, function (error) {
    //出现401，返回登录页面
    if (error.response.status === 401) {
        router.push("/login")
    } else if ( error.response.status === 429) {
        router.push("/429")
    }
    return Promise.reject(error)
});

export default service;